<?php
require_once 'TVB/pms/pdes/common/util/Unpublished.class.php';
require_once 'TVB/VideoInfrastructure/Client/requires.php';
require_once 'TVB/ImageInfrastructure/Client/requires.php';

class VideoSwitchAction extends BaseAction
{
	public $id;
    public $ut;

    public function execute()
    {
		if (count($this->_errors) > 0)
		{
			return "filter-error";
		}

		if (isset($_REQUEST['delete']))
		{
			return $this->delete_video();
		}
		elseif (isset($_REQUEST['recover']))
		{
			return	$this->recover_video();
		}
		elseif (isset($_REQUEST['edit']))
		{
			return $this->edit_video();
		}
        elseif (isset($_REQUEST['unpublish']))
        {
            return  $this->unpublish_video();
        }
        elseif (isset($_REQUEST['unpublish_premium']))
        {
           return  $this->unpublish_premium_video();
        }
    }

	/**
	 * recover video
	 */
	private function recover_video()
	{
		Model::$dbo['w']->beginTransaction();
		try
		{
			//for bug:7529     recover the chapter of video    #modified by Albie Lu on 2011-10-26
			$arr = array(':status'=>AC::STATUS_DELETED,':status2'=>AC::STATUS_NEW,':video_id'=>$this->id);
			Model::updateBySql("update chapter set status=:status2 where video_id=:video_id and status=:status",$arr);
		
			$video = new video();
			$video = $video->get($this->id);
			$video->status = AC::STATUS_NEW;
			$update_id = Model::$dbo['w']->save($video);

			if ($update_id > 0)
			{
				Model::$dbo['w']->commit();
				$this->addInfo("video:".AC::LOG_OP_RECOVER.":".$this->id, "video recover success", json_encode($video));

				if($video->ii_status != AC::IMAGE_STATUS_FAILED){
					$ii = new ImageInfrastructureClient();
					$ok_ii = $ii->recoverSource($video->ii_id);
				}else{
					$ok_ii = 1;
				}

				if ($ok_ii >= 0)
				{
					// recover remote file
					if($video->vi_status != AC::VIDEO_STATUS_FAILED){
						$vi = new VideoInfrastructureClient();
						$ok_vi = $vi->recoverSource($video->vi_id);
					}else{
						$ok_vi = 1;
					}

					if($ok_vi < 0)
					{
						$this->addError("video:".AC::LOG_OP_RECOVER.":".$this->id, "VI Server Error, Recover video Fails!", json_encode($video));
					}
					else
					{
						$this->addInfo("video:".AC::LOG_OP_RECOVER.":".$this->id, "video recover success", json_encode($video));
					}
				}
				else
				{
					$this->addError("video:".AC::LOG_OP_RECOVER.":".$this->id, "VI Server Error, Recover video Fails!", json_encode($video));
				}
			}
			else
			{
				Model::$dbo['w']->rollBack();
				$this->addError("video:".AC::LOG_OP_RECOVER.":".$this->id, "video recover error", json_encode($video));
			}

			$this->setData('type', 'video');
			return 'recover';
		}
		catch(Exception $e)
		{
			error_log("recover video: {$this->id}" . var_export($e,1));
			Model::$dbo['w']->rollBack();
			return 'system-error';
		}
	}

	///** Merge to unpublish_video
    private function unpublish_premium_video()
    {
	    try
		{
			$video = new video();
			$video = $video->get($this->id);

			$log_id = CommonUtils::save_log('video', $this->id, AC::LOG_OP_UNPUBLISHING, json_encode($video));
			$unpublish_flag = P_Unpublished::send('video', $this->id, $video->programme_id, $log_id);

			$this->setData('type', 'video');
			return 'unpublish';
		}
		catch(Exception $e)
		{
			error_log("unpublish video: {$this->id}" . var_export($e,1));

			return 'system-error';
		}
    }
	// */
	/**
	 * unpublish video
	 */
    private function unpublish_video()
	{
		try
		{
			$video = new video();
			$video = $video->get($this->id);

			$log_id = CommonUtils::save_log('video', $this->id, AC::LOG_OP_UNPUBLISHING, json_encode($video));
			Unpublished::send('video', $this->id, $video->programme_id, $log_id);

			// donald 2011-03-21
			//if(CommonUtils::checkLastPublishStatus('video',$this->id,AC::STATUS_PUBLISH_PREMIUM)){
			//	#$log_id = CommonUtils::save_log('video', $this->id, AC::LOG_OP_PREMIUM_UNPUBLISHING, json_encode($video));
			//	P_Unpublished::send('video', $this->id, $video->programme_id, $log_id);
			//}

			$this->setData('type', 'video');
			return 'unpublish';
		}
		catch(Exception $e)
		{
			error_log("unpublish video: {$this->id}" . var_export($e,1));

			return 'system-error';
		}
    }

	/**
	 * edit video
	 */
    private function edit_video()
    {
		try
		{
			$video = new video();
			$video = $video->get($this->id);
			if ($video->status == AC::STATUS_DELETED)
			{
				$this->setData('act', 'view');
				$this->setData('id', $this->id);
				$this->setData('error_code', '0108');
				return 'logic-error';
			}

			$this->setData('id', $this->id);
			$this->setData('type', 'video');
			$this->setData('act', 'edit');

			return 'edit';
		}
		catch(Exception $e)
		{
			error_log("edit video: {$this->id}" . var_export($e,1));

			return 'system-error';
		}
    }

	/**
	 * delete video
	 */
    private function delete_video()
    {
		Model::$dbo['w']->beginTransaction();
		try{
			$obj = new video();
			$obj = $obj->get($this->id);
			$obj->status = AC::STATUS_DELETED;
			$obj->modified_at = date("Y-m-d H:i:s");
			$obj->modified_by = CommonUtils::get_user();
			$ok = Model::$dbo['w']->save($obj);

			if ($ok > 0)
			{
				$this->addInfo("video:".AC::LOG_OP_DELETE.":".$this->id, "video delete success", json_encode($obj));

				// ==>by donald
				$obj->delete_change_log();

				$video_language = new video_language;
				$video_language_list = $video_language->getAll($this->id);
				foreach ($video_language_list as $video_language) {
					$video_language->delete_change_log();
				}

				$video_premium = video_premium::getAll($this->id); 
				if (!empty($video_premium)) {
					$video_premium->delete_change_log();
				}
				
				$video_ppv = new video_ppv;
				$video_ppv = $video_ppv->get($this->id);
				if (!empty($video_ppv)) {
					$video_ppv->delete_change_log();
				}

				$video_subscription = new video_subscription;
				$video_subscription = $video_subscription->get($this->id);
				if (!empty($video_subscription)) {
					$video_subscription->delete_change_log();
				}

				$video_export_list = video_export::getAll($this->id);
				foreach ($video_export_list as $video_export) {
					$video_export->delete_change_log();
				}

				$folder_item = new folder_item;
				$folder_item_list = $folder_item->getAll(
					array(
						'type' => 'video', 
						'item_id' => $this->id
					)
				);
				foreach ($folder_item_list as $folder_item) {
					$folder_item->delete_change_log();
				}

				$folder_item_language = new folder_item_language();
				$folder_item_language_list = $folder_item_language->getAllByType('video', $this->id);
				foreach ($folder_item_language_list as $folder_item_language) {
					$folder_item_language->delete_change_log();
				}

				$episode_video = new episode_video;
				$episode_video_list = $episode_video->getAll(array('video_id' => $this->id));
				foreach ($episode_video_list as $episode_video) {
					$episode_video->delete_change_log();
				}

				$chapter = new chapter;
				$chapter_list = $chapter->getAll(
					array(
						'video_id' => $this->id,
						'status' => AC::STATUS_NEW
					)
				);
				foreach ($chapter_list as $chapter) {
					$chapter->delete_change_log();

					$chapter_language = new chapter_language;
					$chapter_language_list = $chapter_language->getAll(
						array(
							'chapter_id' => $chapter->id
						)
					);

					foreach ($chapter_language_list as $chapter_language) {
						$chapter_language->delete_change_log();
					}

        			$chapter_tvbcom_category = new chapter_tvbcom_category();
					$chapter_tvbcom_category_list = $chapter_tvbcom_category->getAll($chapter->id);
					foreach ($chapter_tvbcom_category_list as $chapter_tvbcom_category) {
						$chapter_tvbcom_category->delete_change_log();
					}
				}
				// by donald<==
				
				// delete folder item and language record
				$item_folders = 'SELECT id FROM folder WHERE folder_item_type_id=:type';
				Model::updateBysql("DELETE FROM `folder_item-language` WHERE item_id=:id AND folder_id IN($item_folders)",array(':type'=>AC::FOLDER_TYPE_VIDEO,':id'=>$this->id));
				Model::updateBysql("DELETE FROM `folder_item` WHERE item_id=:id AND folder_id IN($item_folders)",array(':type'=>AC::FOLDER_TYPE_VIDEO,':id'=>$this->id));
				Model::updateBySql('delete from `episode-video` where video_id = :vid',   array(':vid'=>$this->id));
				Model::updateBySql('update chapter set status=:st where video_id = :vid', array(':vid'=>$this->id,':st'=>AC::STATUS_DELETED)); 

				Model::$dbo['w']->commit();

				// remove remote file
				if($obj->ii_status != AC::IMAGE_STATUS_FAILED){
					$ii = new ImageInfrastructureClient();
					$removed_image = $ii->DeleteSource($obj->ii_id);
				}else{
					$removed_image = 1;
				}
				if($obj->vi_status != AC::VIDEO_STATUS_FAILED){
					$vi = new VideoInfrastructureClient();
					$removed_video = $vi->DeleteSource($obj->vi_id);
				}else{
					$removed_video = 1;
				}

				if($removed_image < 0 || $removed_video < 0)
				{
					$this->addError("video:".AC::LOG_OP_DELETE.":".$this->id, "II or VI Server Error, Delete video Fails!", json_encode($obj));
				}
				else
				{
					$this->addInfo("video:".AC::LOG_OP_DELETE.":".$this->id, "video delete success", json_encode($obj));
				}
			}
			else
			{
				$this->addError("video:".AC::LOG_OP_DELETE.":".$this->id, "video delete error", json_encode($obj));
			}

			$this->setData('type', 'video');
			return 'delete';
		}
		catch(Exception $e)
		{
			error_log("Delete Video Fails: {$this->id}" . var_export($e,1));

			Model::$dbo['w']->rollBack();

			return 'system-error';
		}
    }
}
?>
